
const request = require('../../../utils/request');
const { watch, computed }  = require('../../../utils/computed')

Component({

    behaviors: [],

    properties: {
        'list': Array,
        'designDish': Array,
        'clearFlag': Boolean,
        'clearCart': Array,
        'canBuy': Boolean   //查看菜谱
    },
    data: {
        listIndex: 0,
        listSub: 0,
        allHeight:[],
        num: 0,
        localFoods:'',
        imgUrl: request.imgUrl
    }, // 私有数据，可用于模版渲染

    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {},
    moved: function () { },
    detached: function () { },
    ready() {
        var query = wx.createSelectorQuery().in(this)
        var that = this 
        let time1 = setInterval(function(){
            if(that.data.designDish.length == 0){
                console.log(that.data.designDish)
            }else{
                clearInterval(time1)
                that.setData({
                    designDish: that.data.designDish
                })
                console.log(that.data.designDish)
                query.selectAll('.item-con').boundingClientRect(function(res){
                        //console.log(res)
                        var heightArray = [];
                        let height = 0;
                        that.data.allHeight.push(height)
                        for(var i=0;i<res.length;i++){
                            height += res[i].height;
                            that.data.allHeight.push(Math.floor(height*10)/10)
                        }
                        that.setData({
                            allHeight:that.data.allHeight
                        })
                        
                }).exec();
            }  
        },2000)
        // let time2 = setInterval(function(){
        //     if(that.data.designDish == ''){
        //         time2;
        //     }else{
        //         clearInterval(time2)
        //         if(wx.getStorageSync('foods')){
        //             that.data.designDish.forEach(val => {
        //                 val.dish.forEach((res,index) => {
        //                     wx.getStorageSync('foods').forEach(food => {
        //                         if(res.id == food.id){
        //                             res = food
                                    
        //                         }
        //                     })
        //                     val.dish[index] = res
        //                 })
        //             });
        //         }else{
        //             return false
        //         }

        //         that.setData({
        //             designDish: that.data.designDish
        //         })
        //     }
            
        // },50)

        watch(this,{
            clearFlag: function() {
                this.setData({
                    designDish: this.data.clearCart
                })
                console.log(this.data.clearFlag)
            }
        })
        
    },

    methods: {
        scrollHandle(e) {
            var scrollY = e.detail.scrollTop + 0.011;
            var listHeight = this.data.allHeight;
            for(let i=0;i<listHeight.length;i++){
                let height1 = listHeight[i];
                let height2 = listHeight[i+1];
                if(scrollY >= height1 && scrollY < height2){
                    //console.log(scrollY)
                    this.setData({
                        listIndex: i
                    })
                }
            }
        },
        listTab(e) {
            //console.log(this.data.allHeight)
            let height = this.data.allHeight[e.target.dataset.id];
            //console.log(height)
            this.setData({
                listSub: height,
                listIndex: e.target.dataset.id
            })
        },
        foodMess(e) {
            //console.log(e.detail);
            this.triggerEvent('foodMessList', {allFoods:e.detail},{ bubbles: true })
        },
        clickRegular(e) {
            console.log(e.target.dataset.foods);
            this.triggerEvent('reguFoods', e.target.dataset.foods)
        },

        goodsDetail(e) {
            this.triggerEvent('goodsDetail', e.currentTarget.dataset.foods)
        }
    }

})